package com.example.demo.clid;

import com.example.demo.dao1.StuJpaDao;
import com.example.demo.entity.Stucu;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.MultipartFilter;

import javax.annotation.Resource;
import javax.persistence.Id;
import java.io.File;
import java.io.IOException;
import java.net.IDN;
import java.util.List;

import static javax.swing.text.html.parser.DTDConstants.ID;

@RestController
public class JpaController {
    @Resource
    StuJpaDao stuJpaDao;

    public JpaController() {
    }

    @GetMapping({"/findByNameNotNull"})
    public List<Stucu> findByNameNotNull() {
        return this.stuJpaDao.findByNameNotNull();
    }

    @GetMapping({"/getOne"})
    public Stucu getOne(int id) {
        return this.stuJpaDao.getOnes(id);
    }

    @GetMapping({"/getOne1"})
    public Stucu getOnes(int id) {
        return this.stuJpaDao.getOneBy(id);
    }

    @GetMapping({"/getOneJpa"})
    public Stucu getOneJpa(int id) {
        return (Stucu)this.stuJpaDao.findById(id).get();
    }

    @GetMapping({"/deleteJpa"})
    public String deleteJpa(int id) {
        this.stuJpaDao.deleteById(id);
        return "删除成功";
    }

    @GetMapping({"/insertJpa"})
    public String insertJpa(String name, String sex, int classId) {
        Stucu student = new Stucu();
        student.setName(name);
        student.setSex(sex);
        student.setClassId(classId);
        Stucu result = (Stucu)this.stuJpaDao.save(student);
        return result != null ? "插入成功" : "插入失败";
    }

    @GetMapping({"/updateJpa"})
    public String updateJpa(int id, String name) {
        Stucu student = (Stucu)this.stuJpaDao.findById(id).get();
        student.setName(name);
        Stucu result = (Stucu)this.stuJpaDao.save(student);
        return result != null ? "更新成功" : "更新失败";
    }

    @GetMapping({"/findAllJpa"})
    public List<Stucu> findAllJpa() {
        return this.stuJpaDao.findAll();
    }

    @GetMapping({"/findAllJpaByName"})
    public List<Stucu> findAllJpa1(String name, int age) {
        return age == 0 ? this.stuJpaDao.findByNameContaining(name) : this.stuJpaDao.findByNameContainingAndAge(name, age);
    }

    @GetMapping({"/findAllJpaByNam"})
    public List<Stucu> findAllJpa1(String sex) {
        return this.stuJpaDao.findBySex(sex);
    }

    @GetMapping({"/findAllJpaByNamem"})
    public List<Stucu> findAllJpa1(int classId) {
        return this.stuJpaDao.findByClassId(classId);
    }

    @GetMapping({"/findByNameAndName"})
    public List<Stucu> findByNameAndName(String name, String name2) {
        return this.stuJpaDao.findByNameOrName(name, name2);
    }
    @PostMapping({"/uploadFile"})
    public String uploadFile(MultipartFile file, int id) throws IOException {
        String filename = file.getOriginalFilename();
        String dirPath = "D:\\file\\";
        File filePath = new File(dirPath);
        if (!filePath.exists()){
            filePath.mkdirs();
        }
        File newFile =new File(dirPath+filename);
        file.transferTo(newFile);
        Stucu stucu =stuJpaDao.findById(id).get();
        stucu.setTouxiang("http://192.168.31.132:8080/images/"+filename);
        stuJpaDao.save(stucu);
        return "上传成功";
    }
}
